﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using YDCommon;
using YDServices;
using YDWin.Base;
using YDModel;
using YDModel.Entity;

namespace YDWin
{
    public partial class LoginForm : AddForm
    {
        EmployeeService svr = new EmployeeService();
        public LoginForm()
        {
            InitializeComponent(); 
        }

        private void btnSignIn_Click(object sender, EventArgs e)
        {
            WaitDialogForm sf = new WaitDialogForm("", "正在登录......");
            try
            {
                T_Employee tu = new T_Employee();

                string userCode = txtUserCode.Text.Trim().ToString();
                if (userCode.Equals(""))
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "账号不能为空!\n");
                    return;
                }
                string passWord = txtPassword.Text.Trim().ToString();
                if (passWord.Equals(""))
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "密码不能为空!\n");
                    return;
                }
                tu.UserCode = userCode;
                tu.UserPassword = passWord; 
   
                var outTu = svr.LoginEmployee(tu);
                sf.Close();
                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtError, "登录失败：" + svr.Errors);
                    svr.Errors.Clear();
                    return;
                }
                else
                {
                    T_User user = new T_User();
                    user.ID = outTu.ID;
                    user.UserCode = outTu.UserCode;
                    user.UserName = outTu.EmployeeName;
                    Global.User = user;
                }
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch(Exception ex)
            {
                sf.Close();
                Utils.ShowPrompt(MessageType.mtError, "登录失败：" + ex.Message.ToString());
            }
      
        }
 
 
        private void LoginForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            //按esc关闭界面
            if (e.KeyChar == (char)Keys.Enter)
            {
                btnSignIn_Click(sender,e);
            }
        }
    }
}